using Autenticazione;
using System;
using System.Data.SqlClient;

namespace BusinessObject
{
	internal class Club
	{
		public string EMail;
		public string ClubDescription;
		public string Citta;
		public int IdRegione;
		public string Telefono;
		public Club()
		{
		}
		public Club(string email, string club, string citta, int idregione, string telefono)
		{
			this.EMail = email;
			this.ClubDescription = club.Replace("'", "''");
			this.Citta = citta.Replace("'", "''");
			this.IdRegione = idregione;
			this.Telefono = telefono;
		}
		public Club GetClub(string email)
		{
			Club c = null;
			string strSQL = string.Format("select * from Club where Email = '{0}'", email);
			DB db = new DB();
			SqlDataReader dr = db.Reader(strSQL);
			if (dr == null)
			{
				return c;
			}
			if (dr.Read())
			{
				c = new Club(dr["Email"].ToString(), dr["Club"].ToString(), dr["Citta"].ToString(), int.Parse(dr["IdRegione"].ToString()), dr["Telefono"].ToString());
			}
			db.CloseConnection();
			return c;
		}
		public bool Salva()
		{
			string strSQL = string.Empty;
			if (this.GetClub(this.EMail) == null)
			{
				strSQL = string.Format("insert into Club (EMail, Club, Citta, IdRegione, Telefono) values ('{0}', '{1}', '{2}', {3}, '{4}')", new object[]
				{
					this.EMail, 
					this.ClubDescription, 
					this.Citta, 
					this.IdRegione.ToString(), 
					this.Telefono
				});
			}
			else
			{
				strSQL = string.Format("Update Club set Club = '{0}', Citta = '{1}', IdRegione = {2}, Telefono = '{3}' where EMail = '{4}'", new object[]
				{
					this.ClubDescription, 
					this.Citta, 
					this.IdRegione.ToString(), 
					this.Telefono, 
					this.EMail
				});
			}
			DB db = new DB();
			return db.ExecuteNonQuery(strSQL);
		}
	}
}
